// src/stores/auth.js
import {defineStore} from 'pinia'
import {computed, ref} from 'vue'

export const useAuthStore = defineStore('auth', () => {
        // 状态定义
        const token = ref('')
        const accountId = ref(null)

        // 纯状态操作方法
        const setAuthData = (newToken, newAccount) => {
            token.value = newToken
            accountId.value = newAccount
        }

        const clearAuthData = () => {
            token.value = ''
            accountId.value = null
            localStorage.removeItem('auth')
        }

        // 计算属性
        const isAuthenticated = computed(() => !!token.value)

        return {
            token,
            accountId,
            isAuthenticated,
            setAuthData,
            clearAuthData
        }
    }, {
        persist: true
    }
)